#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "mysql.h"


#define C_DB_SERVER_IP 		"127.0.0.1"
#define C_DB_SERVER_PORT 	3306
#define C_DB_USERNAME 		"root"
#define C_DB_PASSWORD 		"root"
#define C_DB_DEFAULT_DB 	"temp_waf"

int c_db_select(MYSQL *mysql,char *sql);
int c_db_sql(MYSQL *mysql,char *sql);
int print_sql(char *sql);
int print_result(MYSQL_RES *res);
int op_dbs();
int mysql_input();
int switch_fun(MYSQL *mysql,char *sql,char *first_str);
int c_db_show_tables_dbs(MYSQL *mysql,char *sql);
int c_db_drop_tables_dbs(MYSQL *mysql,char *sql);

int insert_mysql(char *id,char *time,float *temper)
{

	MYSQL 			mysql;
	char			insert_sql[1024];
	char			sql;
	char			buf[1024];
	char			*ptr=NULL;
	char			first_str[512];


	//mysql_init
	if(mysql_init(&mysql)==NULL)
	{
		printf("myslq_init failure:%s\n",mysql_error(&mysql));
		return -1;
	}

	printf("\n");
	printf("-------------mysql_init ok!-----------------\n");


	//mysql_real_connect
	if(!mysql_real_connect(&mysql,
	C_DB_SERVER_IP,C_DB_USERNAME,
	C_DB_PASSWORD,C_DB_DEFAULT_DB,
	C_DB_SERVER_PORT,NULL,0))
	{
		printf("mysql_real_connect failure:%s\n",mysql_error(&mysql));
		return -2;
	}
/*
	sprintf(insert_sql,"insert into rpi_temp(id,time,temp) values('%s','%s','%.3f')",id,time,*temper);

	if(mysql_real_query(&mysql,insert_sql,strlen(insert_sql)))
	{
		printf("'%s' failure:%s!\n",insert_sql,mysql_error(&mysql));
		return -2;
		}
		*/
	sprintf(insert_sql,"create table if not exists %s(time char(32) not null,temp float(5,3) not null)",id);

	if(mysql_real_query(&mysql,insert_sql,strlen(insert_sql)))
	{
		printf("'%s' failure:%s!\n",insert_sql,mysql_error(&mysql));
		return -2;
	}

	memset(insert_sql,0,sizeof(insert_sql));
	sprintf(insert_sql,"insert into %s(time,temp) values('%s','%.3f')",id,time,*temper);
	if(mysql_real_query(&mysql,insert_sql,strlen(insert_sql)))
	{
		printf("'%s' failure:%s!\n",insert_sql,mysql_error(&mysql));
		return -2;
	}



#if 0
	printf("\n");
	printf("Welcome to the Mysql monitor!\n");
	op_dbs();
	while(1)
	{
		printf("\n");
		memset(buf,0,sizeof(buf));
		memset(first_str,0,sizeof(buf));
		printf("mysql>");
		fgets(buf,1024,stdin);

		mysql_input(buf,&first_str);
		switch_fun(&mysql,buf,first_str);

		printf("\n");
	}
#endif

	return 0;

}

#if 0

int c_db_sql(MYSQL *mysql,char *sql)
{
	if(mysql == NULL)
	{
		printf("'%s' failure:%s\n",sql,mysql_error(mysql));
		return -1;
	}

//	print_sql(sql);
	if(mysql_real_query(mysql,sql,strlen(sql)))
	{
		printf("'%s' failure:%s!\n",sql,mysql_error(mysql));
		return -2;
	}

	printf("-------Query OK,%ld rows affected--------\n",(long)mysql_affected_rows(mysql));


}
#endif

#if 0
int op_dbs()
{
	printf("+-----------------+---------------+\n");
	printf("|     usage       | describe table|\n");
	printf("|-----------------|---------------|\n");
	printf("|     delete      |drop tables/dbs|\n");
	printf("|-----------------|---------------|\n");
	printf("|      select     |               |\n");
	printf("|-----------------|---------------|\n");
	printf("| show tables/dbs |               |\n");
	printf("|-----------------|---------------|\n");
	printf("|  update table   |               |\n");
	printf("|-----------------|---------------|\n");
	printf("|create table/dbs |               |\n");
	printf("|-----------------|---------------|\n");
	printf("|     insert      |               |\n");
	printf("+-----------------+---------------+\n");
}

#endif



#if 0
int c_db_drop_tables_dbs(MYSQL *mysql,char *sql)
{
	MYSQL_RES 		*res;
	char			*ptr=NULL;
//	printf("sql in show :%s\n",sql);

	//	print_sql("show tables");
	if(strstr(sql,"tables"))
	{
		res=mysql_list_tables(mysql,NULL);
		if(!res)
		{
			printf("ERROR(drop tables):%s\n",mysql_error(mysql));
			return -1;
		}

		print_result(res);

	}
	if((ptr=strstr(sql,"databases"))!=NULL)
	{
		res=mysql_list_dbs(mysql,NULL);
		if(!res)
		{
			printf("ERROR(drop databases):%s\n",mysql_error(mysql));
			return -1;
		}

		print_result(res);
	}

}

int c_db_show_tables_dbs(MYSQL *mysql,char *sql)
{
	MYSQL_RES 		*res;
	char			*ptr=NULL;
//	printf("sql in show :%s\n",sql);

	//	print_sql("show tables");
	if(strstr(sql,"tables"))
	{
		res=mysql_list_tables(mysql,NULL);
		if(!res)
		{
			printf("ERROR(show tables):%s\n",mysql_error(mysql));
			return -1;
		}

		print_result(res);

	}
	if((ptr=strstr(sql,"databases"))!=NULL)
	{
		res=mysql_list_dbs(mysql,NULL);
		if(!res)
		{
			printf("ERROR(show databases):%s\n",mysql_error(mysql));
			return -1;
		}

		print_result(res);
	}

}


#if 1


int c_db_select(MYSQL *mysql,char *sql)
{
	MYSQL_RES		*result;
	my_ulonglong		row;
	MYSQL_FIELD		*field;
	MYSQL_FIELD		*fields;
	MYSQL_ROW		row2;
	unsigned int		i;
	unsigned int		num_fields;

	//select
	printf("\n");

	if(mysql == NULL)
	{
		printf("%s\n",mysql_error(mysql));
		return -1;
	}

	if(mysql_real_query(mysql,sql,strlen(sql)))
	{
		printf("mysql_real_query failure:%s\n",mysql_error(mysql));
		return -2;
	}
	else
	{
		result = mysql_store_result(mysql);
		if(result == NULL)
		{
			printf("mysql_store_result failure:%s\n",mysql_error(mysql));
			return -3;
		}

		row = mysql_num_rows(result);
		printf("%lld row(s) in set\n",row);

		printf("\n");

		num_fields = mysql_num_fields(result);
		fields= mysql_fetch_fields(result);
		for(i=0;i<num_fields;i++)
		{
			printf("\t%s\t",fields[i].name);
		}
		

		printf("\n");
		//打印select结果
		print_result(result);
	
	}
}
#endif

int print_result(MYSQL_RES *res)
{
	unsigned long		*lengths;
	unsigned int		num_fields;
	MYSQL_ROW		row;

	num_fields = mysql_num_fields(res);
	while(row=mysql_fetch_row(res))
	{
		lengths = mysql_fetch_lengths(res);
		for(int i=0;i<num_fields;i++)
		{
			printf("\t|%.*s\t",(int)lengths[i],row[i]?row[i]:"NULL");
		}

		printf("\n");

	}
	mysql_free_result(res);
}


//printf mysql>
int print_sql(char *sql)
{
	printf("mysql>%s\n",sql);

}

int mysql_input(char *buf,char *first_str)
{
	char		*ptr;
	char		*start_ptr=NULL;
	char		*end_ptr=NULL;
	char		*p=NULL;


	while(1)
	{
		if((ptr=strstr(buf,"select"))!=NULL)
		{
			break;
		}
		if((ptr=strstr(buf,"show"))!=NULL)
		{
			break;
		}
		if((ptr=strstr(buf,"delete"))!=NULL)
		{
			break;
		}
		if((ptr=strstr(buf,"insert"))!=NULL)
		{
			break;
		}
		if((ptr=strstr(buf,"update"))!=NULL)
		{
			break;
		}
		if((ptr=strstr(buf,"create"))!=NULL)
		{
			break;
		}
		if((ptr=strstr(buf,"use"))!=NULL)
		{
			break;
		}
		if((ptr=strstr(buf,"describe"))!=NULL)
		{
			break;
		}
		if((ptr=strstr(buf,"drop"))!=NULL)
		{
			break;
		}
	}
	start_ptr = ptr;
	

	while(!isblank(*ptr))
	{
		ptr++;
	}

	end_ptr=ptr;
	memcpy(first_str,start_ptr,end_ptr-start_ptr);

//	printf("first_str:%s\n",first_str);
	


}
int switch_fun(MYSQL *mysql,char *sql,char *first_str)
{
/*	printf("\n");
	printf("in switch_fun ,first_str = %s\n",first_str);
	printf("\n");
*/
	if(!(strcmp(first_str,"select")))
	{
		c_db_select(mysql,sql);
	}
	else if(!(strcmp(first_str,"describe")))
	{
		c_db_select(mysql,sql);
	}
	else if(!(strcmp(first_str,"show")))
	{
		c_db_show_tables_dbs(mysql,sql);
	}
	else if(!(strcmp(first_str,"drop")))
	{
		c_db_drop_tables_dbs(mysql,sql);
	}

	else
	{
		c_db_sql(mysql,sql);
	}


}
#endif





